//
//  CashRecord.m
//  RRQS
//
//  Created by kyjun on 16/4/22.
//
//

#import "CashRecord.h"
#import "Pager.h"
#import "InputOrOutCell.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>


@interface CashRecord ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property(nonatomic,strong) Pager* page;
@property(nonatomic,strong) NSString* cellIdentifier;

@end

@implementation CashRecord

- (void)viewDidLoad {
    [super viewDidLoad];
    self.cellIdentifier = @"InputOrOutCell";
    [self.tableView registerClass:[InputOrOutCell  class] forCellReuseIdentifier:self.cellIdentifier];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    [self refreshDataSource];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark =====================================================  Data source
-(void)queryData{
    AFHTTPSessionManager *mgr=[AFHTTPSessionManager manager];
    NSDictionary* arg = @{@"ince":@"get_emp_depositlog",@"emp_id":self.Identity.userInfo.userID,@"page":[WMHelper integerConvertToString:self.page.pageIndex]};
    [mgr POST:kHost parameters:arg progress:nil success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject) {
        [WMHelper outPutJsonString:responseObject];
        if(self.page.pageIndex==1)
            [self.page.arrayData removeAllObjects];
        NSInteger flag = [[responseObject objectForKey:@"status"]integerValue];
        NSArray* emptyArray = [NSArray array];
        if(flag ==1){
            emptyArray = [responseObject objectForKey:@"list"];
            self.page.recordCount = [[responseObject objectForKey:@"total"] integerValue];
            self.page.pageSize = [[responseObject objectForKey:@"number"]integerValue];
            NSInteger empty = self.page.recordCount % self.page.pageSize;
            if(empty == 0){
                self.page.pageCount = self.page.recordCount/self.page.pageSize;
            }else{
                self.page.pageCount = self.page.recordCount/self.page.pageSize+1;
            }
            if(![WMHelper isNULLOrnil:emptyArray]){
                [emptyArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    MCashMoney* item = [[MCashMoney alloc]initWithItem:obj];
                    [self.page.arrayData addObject:item];
                }];
                
            }
        }else {
            //[self alertHUD:[responseObject objectForKey:@"fail"]];
        }
        [self.tableView reloadData];
        if(self.page.pageCount<=self.page.pageIndex){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        if(self.page.pageIndex==1){
            [self.tableView.mj_header endRefreshing];
        }
    } failure:^(NSURLSessionDataTask *_Nonnull operation, NSError * _Nonnull error) {
        [self alertHUD:@"网络异常"];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

-(void)refreshDataSource{
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page.pageIndex = 1;
        [weakSelf queryData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page.pageIndex++;
        [weakSelf queryData];
    }];
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark =====================================================  <UITableViewDataSource>
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.page.arrayData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InputOrOutCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    [cell loadData: self.page.arrayData[indexPath.row]];
    return cell;
}

#pragma mark =====================================================  <UITableVeiwDelegate>
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45.f;
}
#pragma mark =====================================================  DZEmptyData 协议实现
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    return [[NSAttributedString alloc] initWithString:@"暂无提现记录" attributes:@{NSFontAttributeName :[UIFont boldSystemFontOfSize:17.0],NSForegroundColorAttributeName:[UIColor grayColor]}];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return roundf(self.tableView.frame.size.height/10.0);
}
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}


#pragma mark =====================================================  property package
-(Pager *)page{
    if(!_page){
        _page = [[Pager alloc]init];
        _page.pageIndex = 1;
    }
    return _page;
}

@end
